<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"> <style>
	.KEYW {color: #933;}
	.COMM {color: #bbb; font-style: italic;}
	.NUMB {color: #393;}
	.STRN {color: #393;}
	.REGX {color: #339;}
	.line {border-right: 1px dotted #666; color: #666; font-style: normal;}
	</style></head><body><pre><span class='line'>  1</span> <span class="COMM">/**
<span class='line'>  2</span>  * Constructs a default position constraint using the &lt;tt>fix&lt;/tt> attribute.
<span class='line'>  3</span>  * An optional position function can be specified to determine how the fixed
<span class='line'>  4</span>  * position per-particle is determined.
<span class='line'>  5</span>  *
<span class='line'>  6</span>  * @class Constraints particles to a fixed position. The fixed position per
<span class='line'>  7</span>  * particle is determined using a given position function, which defaults to
<span class='line'>  8</span>  * &lt;tt>function(d) d.fix&lt;/tt>.
<span class='line'>  9</span>  *
<span class='line'> 10</span>  * &lt;p>If the position function returns null, then no position constraint is
<span class='line'> 11</span>  * applied to the given particle. Otherwise, the particle's position is set to
<span class='line'> 12</span>  * the returned position, as expressed by a {@link pv.Vector}. (Note: the
<span class='line'> 13</span>  * position does not need to be an instance of &lt;tt>pv.Vector&lt;/tt>, but simply an
<span class='line'> 14</span>  * object with &lt;tt>x&lt;/tt> and &lt;tt>y&lt;/tt> attributes.)
<span class='line'> 15</span>  *
<span class='line'> 16</span>  * &lt;p>This constraint also supports a configurable alpha parameter, which
<span class='line'> 17</span>  * defaults to 1. If the alpha parameter is in the range [0,1], then rather than
<span class='line'> 18</span>  * setting the particle's new position directly to the position returned by the
<span class='line'> 19</span>  * supplied position function, the particle's position is interpolated towards
<span class='line'> 20</span>  * the fixed position. This results is a smooth (exponential) drift towards the
<span class='line'> 21</span>  * fixed position, which can increase the stability of the physics simulation.
<span class='line'> 22</span>  * In addition, the alpha parameter can be decayed over time, relaxing the
<span class='line'> 23</span>  * position constraint, which helps to stabilize on an optimal solution.
<span class='line'> 24</span>  *
<span class='line'> 25</span>  * @param {function} [f] the position function.
<span class='line'> 26</span>  */</span><span class="WHIT">
<span class='line'> 27</span> </span><span class="NAME">pv.Constraint.position</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">f</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 28</span> </span><span class="WHIT">  </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">a</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="COMM">// default alpha</span><span class="WHIT">
<span class='line'> 29</span> </span><span class="WHIT">      </span><span class="NAME">constraint</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 30</span> 
<span class='line'> 31</span> </span><span class="WHIT">  </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">!</span><span class="NAME">arguments.length</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="COMM">/** @ignore */</span><span class="WHIT"> </span><span class="NAME">f</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">p</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT"> </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">p.fix</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 32</span> 
<span class='line'> 33</span> </span><span class="WHIT">  </span><span class="COMM">/**
<span class='line'> 34</span>    * Sets or gets the alpha parameter for position interpolation. If the alpha
<span class='line'> 35</span>    * parameter is in the range [0,1], then rather than setting the particle's
<span class='line'> 36</span>    * new position directly to the position returned by the supplied position
<span class='line'> 37</span>    * function, the particle's position is interpolated towards the fixed
<span class='line'> 38</span>    * position.
<span class='line'> 39</span>    *
<span class='line'> 40</span>    * @function
<span class='line'> 41</span>    * @name pv.Constraint.position.prototype.alpha
<span class='line'> 42</span>    * @param {number} x the new alpha parameter, in the range [0,1].
<span class='line'> 43</span>    * @returns {pv.Constraint.position} this.
<span class='line'> 44</span>    */</span><span class="WHIT">
<span class='line'> 45</span> </span><span class="WHIT">  </span><span class="NAME">constraint.alpha</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 46</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">arguments.length</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 47</span> </span><span class="WHIT">      </span><span class="NAME">a</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">Number</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 48</span> </span><span class="WHIT">      </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">constraint</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 49</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 50</span> </span><span class="WHIT">    </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">a</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 51</span> </span><span class="WHIT">  </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 52</span> 
<span class='line'> 53</span> </span><span class="WHIT">  </span><span class="COMM">/**
<span class='line'> 54</span>    * Applies this constraint to the specified particles.
<span class='line'> 55</span>    *
<span class='line'> 56</span>    * @function
<span class='line'> 57</span>    * @name pv.Constraint.position.prototype.apply
<span class='line'> 58</span>    * @param {pv.Particle} particles particles to which to apply this constraint.
<span class='line'> 59</span>    */</span><span class="WHIT">
<span class='line'> 60</span> </span><span class="WHIT">  </span><span class="NAME">constraint.apply</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">particles</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 61</span> </span><span class="WHIT">    </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">p</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">particles</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">p</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">p</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">p.next</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 62</span> </span><span class="WHIT">      </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">v</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">f</span><span class="PUNC">(</span><span class="NAME">p</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 63</span> </span><span class="WHIT">      </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">v</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 64</span> </span><span class="WHIT">        </span><span class="NAME">p.x</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">v.x</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">p.x</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="WHIT"> </span><span class="NAME">a</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 65</span> </span><span class="WHIT">        </span><span class="NAME">p.y</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">v.y</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">p.y</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="WHIT"> </span><span class="NAME">a</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 66</span> </span><span class="WHIT">        </span><span class="NAME">p.fx</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">p.fy</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">p.vx</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">p.vy</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 67</span> </span><span class="WHIT">      </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 68</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 69</span> </span><span class="WHIT">  </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 70</span> 
<span class='line'> 71</span> </span><span class="WHIT">  </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">constraint</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 72</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 73</span> </span></pre></body></html>